# ContentRecs.py
from MovieLens import MovieLens
from ContentKNNAlgorithm import ContentKNNAlgorithm
from Evaluator import Evaluator
from surprise import NormalPredictor

import random
import numpy as np


def LoadMovieLensData() -> tuple:
    """
    加载 MovieLens 数据集并计算电影的流行度排名。
    
    返回:
        tuple: 包含以下三个元素的元组：
            - ml (MovieLens): MovieLens 数据加载器实例。
            - data: 加载的 MovieLens 数据集。
            - rankings: 电影的流行度排名。
    """
    ml = MovieLens()
    print("Loading movie ratings...")
    data = ml.loadMovieLensLatestSmall()
    print("\nComputing movie popularity ranks so we can measure novelty later...")
    rankings = ml.getPopularityRanks()
    return ml, data, rankings


np.random.seed(0)
random.seed(0)

# Load up common data set for the recommender algorithms
(ml, evaluationData, rankings) = LoadMovieLensData()

# Construct an Evaluator to,you know,evaluate them
evaluator = Evaluator(evaluationData, rankings)

contentKNN = ContentKNNAlgorithm()
evaluator.AddAlgorithm(contentKNN, "ContentKNN")

# Just make random recommendations
Random = NormalPredictor()
evaluator.AddAlgorithm(Random, "Random")

evaluator.Evaluate(False)
evaluator.SampleTopNRecs(ml)
